package ${package.Controller};

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.chub.RespResult;
import com.chub.study.util.SmartWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

import ${package.Service}.${table.serviceName};
import ${package.Entity}.${entity};


/**
 * <p>
 * ${table.comment!}
 * </p>
 *
 * @author ${author}
 * @since ${date}
 */
@Api(tags = "${table.comment!}服务")
@RestController
<#if package.ModuleName == "">
@RequestMapping("/${entity?lower_case}")
<#else>
@RequestMapping("/${package.ModuleName}/${entity?lower_case}")
</#if>
public class ${table.controllerName} {

    @Resource
    private ${table.serviceName} ${table.serviceName ? uncap_first};

    @ApiOperation("创建")
    @PostMapping("/create")
    public RespResult<String> create(@Valid @RequestBody ${entity} ${entity ? uncap_first}) {
        ${table.serviceName ? uncap_first}.save(${entity ? uncap_first});
        return RespResult.success();
    }

    @ApiOperation(value = "删除")
    @PostMapping("/delete")
    public RespResult<String> delete(@RequestParam String id) {
        ${table.serviceName ? uncap_first}.removeById(id);
        return RespResult.success();
    }

    @ApiOperation(value = "修改")
    @PostMapping("/modify")
    public RespResult<String> modify(@Valid @RequestBody ${entity} ${entity ? uncap_first}) {
        ${table.serviceName ? uncap_first}.updateById(${entity ? uncap_first});
        return RespResult.success();
    }

    @ApiOperation(value = "查询详情")
    @GetMapping("/detail")
    public RespResult<${entity}> detail(@RequestParam String id) {
        return RespResult.success(${table.serviceName ? uncap_first}.getById(id));
    }

    @ApiOperation(value = "分页查询")
    @PostMapping("/queryByPage")
    public RespResult<IPage<${entity}>> queryByPage(@RequestBody JSONObject queryObject) {

         SmartWrapper<${entity}> smartWrapper = new SmartWrapper<>();
         smartWrapper.parse(queryObject, ${entity}.class);

         QueryWrapper<${entity}> queryWrapper = smartWrapper.getQueryWrapper();
         IPage<${entity}> page = smartWrapper.generatePage();
         ${table.serviceName ? uncap_first}.page(page, queryWrapper);
         return RespResult.success(page);
    }

}